//
//  TCWarningView.m
//  Pods
//
//  Created by E.F.B on 15-6-2.
//
//

#import "TCWarningView.h"
#import <QuartzCore/QuartzCore.h>

@implementation TCWarningView
{
    UIButton *closeBut;
}

@synthesize titleLabel=_titleLabel;

-(void)dealloc
{
    [_titleLabel release];
    [super dealloc];
}



- (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.frame=CGRectMake((CGRectGetWidth(frame)-220)/2, (CGRectGetHeight(frame)-90)/2, 220, 90);
        //self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        
        self.backgroundColor = [UIColor blackColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 15.0;
        self.layer.borderWidth = 1.0;
        self.alpha = 0.0f;
        
        _titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 220, 85)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _titleLabel.numberOfLines = 2;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:21.0f];
        _titleLabel.text = title;
       
        closeBut=[UIButton buttonWithType:UIButtonTypeCustom];
        closeBut.frame=CGRectMake(self.frame.size.width-33,4,28,28);
        [closeBut setImage:[UIImage imageNamed:@"warning-close.png"] forState:UIControlStateNormal];
        [closeBut addTarget:self action:@selector(clostButton:) forControlEvents:UIControlEventTouchUpInside];
        closeBut.hidden=YES;
        [self addSubview:_titleLabel];
        [self addSubview:closeBut];
        
    }
    
    return self;
}


-(void)clostButton:(id)sender
{
    [self hiddenWarningView];
}

-(void)setTitleText:(NSString *)title
{
    _titleLabel.text = title;

}


-(void)showWarningView
{
    if (self.alpha < 0.2) {
        if (_isClosed && _isStarted)
        {
            [UIView animateWithDuration:1.5  animations:^
             {
                 self.alpha = 0.7;
                 
             } completion:^(BOOL finished)
            {
                 self.alpha = 0.0;
                 
             }];
 
        }else if (!_isStarted)
        {
            closeBut.hidden=!_isClosed; 
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:0.5];
            self.alpha = 0.7;
            [UIView commitAnimations];
        }
    }
    
  }


-(void)hiddenWarningView
{
    self.alpha = 0.0f;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
